package com.heaven.config;

import com.heaven.constant.Constants;
import com.heaven.exception.RRException;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.errors.*;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;

@Configuration
@Slf4j
public class MinioConfig {
    @Value("${minio.server-addr}")
    private String serverAddr;
    @Value("${minio.port}")
    private int port;
    @Getter
    @Value("${minio.bucket1}")
    private String bucket1;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.accessSecret}")
    private String accessSecret;

    @Bean("minioClient")
    public MinioClient initMinioClient() {
        MinioClient minioClient = MinioClient.builder()
                        .endpoint(Constants.HTTP+serverAddr+":"+port)
                        .credentials(accessKey, accessSecret)
                        .build();
        try {
            boolean found = minioClient
                    .bucketExists(BucketExistsArgs
                            .builder()
                            .bucket(bucket1).build());
            if (!found) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket1).build());
            } else {
                log.info("Bucket ${} already exists.", bucket1);
            }
            return minioClient;
        } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException |
                 InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException |
                 XmlParserException e) {
            throw new RRException(e);
        }
    }

    /**
     * 初始化本地minio客户端, 用于服务和minio存储在同一台机器上时使用
     */
//    @Bean("localMinioClient")
//    public MinioClient initLocalMinioClient() {
//        MinioClient minioClient = MinioClient.builder()
//                .endpoint(Constants.HTTP+"127.0.0.1:"+9000)
//                .credentials(accessKey, accessSecret)
//                .build();
//        try {
//            boolean found = minioClient
//                    .bucketExists(BucketExistsArgs
//                            .builder()
//                            .bucket(bucket1).build());
//            if (!found) {
//                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket1).build());
//            } else {
//                log.info("Bucket ${} already exists.", bucket1);
//            }
//            return minioClient;
//        } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException |
//                 InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException |
//                 XmlParserException e) {
//            throw new RRException(e);
//        }
//
//    }
}
